package com.zjh.workflow.flowable.controller;

import com.zjh.common.entity.Response;
import com.zjh.workflow.flowable.entity.ProcessDefineList;
import com.zjh.workflow.flowable.service.ProcessService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.repository.Deployment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/WorkFlow/process")
@Slf4j
public class ProcessController {

    @Resource
    private ProcessEngine processEngine;

    @Resource
    private ProcessService flowableService;

    @Resource
    private ProcessService processService;

    /**
     * 动态部署流程，接收 xml 流程文件
     */
    @PostMapping("/deploy/file")
    @ApiOperation(value = "上传Xml文件路径部署流程")
    public Response<?> deploy(@RequestParam(value = "flowName") String flowName,
                              @RequestParam("file") MultipartFile file) {

        log.info("文件部署流程, 流程名称：" + flowName + "流程文件名称： " + file.getOriginalFilename());

        Deployment deployment = flowableService.deployFlowViaMultiFile(flowName, file);

        return Response.success("部署成功: " + deployment.getId());
    }

    /**
     * 根据本地Xml 部署流程
     * workflow/studentLeave.bpmn20.xml
     */
    @PostMapping("/deploy/xml")
    @ApiOperation(value = "根据本地Xml路径部署流程")
    public Response<?> deployFlow(@RequestParam("xmlPath") String xmlPath, @RequestParam("flowName") String flowName) {
        Deployment deploy = processEngine.getRepositoryService().createDeployment()
                .addClasspathResource(xmlPath) // 部署一个流程
                .name(flowName)
                .deploy();

        return Response.success(deploy.getId());
    }


    /**
     * 根据流程定义Id 获取流程XML 定义文件内容
     */
    @ApiOperation(value = "根据流程定义Id获取流程XML")
    @GetMapping("/getXmlStrByProcessId")
    public Response<?> getXmlStrByProcessId(@RequestParam("processId") String processId) {
        String xmlStr = processService.getProcessDefinitionXmlStrByProcessId(processId);

        return Response.success(xmlStr);
    }

    /**
     * 根据流程实例Id 获取流程XML 定义文件内容
     */
    @ApiOperation(value = "根据流程实例Id获取流程XML")
    @GetMapping("/getXmlStrByProcessInstanceId")
    public Response<?> getXmlStrByProcessInstanceId(@RequestParam("processInstanceId") String processInstanceId) {
        String xmlStr = processService.getProcessDefinitionXmlStrByProcessInstanceId(processInstanceId);

        return Response.success(xmlStr);
    }

    /**
     * 删除部署，会删除部署的实例的所有信息
     */
    @ApiOperation(value = "根据ProcessKey删除部署")
    @PostMapping("/deleteDeployByProcessKey/")
    public Response<?> deleteDeployByProcessKey(@RequestParam("processKey") String processKey) {
        Boolean aBoolean = flowableService.deleteDeployByProcessKey(processKey);
        if (aBoolean) {
            return Response.success();
        } else {
            return Response.error("删除失败");
        }
    }

    /**
     * 删除部署，会删除部署的实例的所有信息
     */
    @PostMapping("/deleteDeployByProcessId")
    @ApiOperation(value = "根据ProcessId删除部署")
    public Response<?> deleteDeployByProcessId(@RequestParam("processId") String processId) {
        Boolean aBoolean = flowableService.deleteDeployByProcessId(processId);
        if (aBoolean) {
            return Response.success();
        } else {
            return Response.error("删除失败");
        }
    }


    /**
     * 获取所有的部署的 流程定义列表
     */
    @ApiOperation(value = "获取部署的流程列表")
    @GetMapping("/list")
    public Response<List<ProcessDefineList>> getAllDefineFlowList() {
        List<ProcessDefineList> allLatestProcessDefinitions = flowableService.getAllLatestProcessDefinitions();

        return Response.success(allLatestProcessDefinitions);
    }

}
